﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint.Administration;

namespace Community.SharePoint.Utilities.Branding
{
    public partial class AppPoolSelection : Form
    {
        private List<SPApplicationPool> appPools;
        public AppPoolSelection()
        {
            InitializeComponent();
            appPools = new List<SPApplicationPool>();
            appPools = BrandingHelper.GetApplicationPools();
            foreach (SPApplicationPool pool in appPools)
            {
                checkedListBoxPools.Items.Add(pool.Name);
            }
        }

        private void buttonRecycle_Click(object sender, EventArgs e)
        {
            foreach (object item in checkedListBoxPools.CheckedItems)
            {
                string name = item as string;
                BrandingHelper.RecycleApplicationPool(name);
                BrandingHelper.WriteToLog(String.Format("Recycled Application Pool - '{0}'", name));
            }
            if (checkedListBoxPools.CheckedItems.Count > 0)
            {
                ShowErrorMessage("Application Pools Recycled", "Operation Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                ShowErrorMessage("Please select a application pool before proceeding.\n", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private static DialogResult ShowErrorMessage(string message, string title, MessageBoxButtons buttons, MessageBoxIcon icon)
        {
            return MessageBox.Show(message, title, buttons, icon, MessageBoxDefaultButton.Button1);
        }
    }
}
